//
//  QBillingContentVC.m
//  QoilPublic
//
//  Created by zhuang on 16/11/10.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QBillingContentVC.h"

@interface QBillingContentVC ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation QBillingContentVC{

    UITableView *_tableView;
    NSArray *_titleArr;
    
    NSString *_start_time;
    NSString *_end_time;
    
    NSDictionary *_dicData;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    CLog(@"===%ld",(long)_index);
    if(_index == 12){
        _titleArr = @[@"总加油量",@"用户级别",@"充值次数",@"充值金额"];
        _start_time = @"";
        _end_time = @"";
    }else{
        _titleArr = @[@"加油量",@"下单次数",@"充值次数",@"充值金额"];
        _start_time = [NSString stringWithFormat:@"%@-%@-01",_yearArr[_index],_monthArr[_index]];
        _end_time = [NSString stringWithFormat:@"%@-%@-%@",_yearArr[_index],_monthArr[_index],_monthLengthArr[_index]];

    }
    [self createTableView];
    
    [self requestData];
    
    CLog(@"star===%@   end====%@",_start_time,_end_time);
}

- (void)createTableView{

    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-kNavHeight-kStatusHeight-45) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
}

#pragma mark --- tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _titleArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identify = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if(!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identify];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.textColor = [UIColor grayColor];
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.textLabel.text = _titleArr[indexPath.row];
    if(_index == 12){
        if(_dicData){
            if(indexPath.row == 0){
                cell.detailTextLabel.text = [NSString stringWithFormat:@"%@升",_dicData[@"oil_cost"]];
            }else if (indexPath.row == 1){
                cell.detailTextLabel.text = _dicData[@"level"];
            }else if (indexPath.row == 2){
                cell.detailTextLabel.text = [NSString stringWithFormat:@"%@次",_dicData[@"recharge_times"]];
            }else if (indexPath.row == 3){
                cell.detailTextLabel.text = [NSString stringWithFormat:@"%@元",_dicData[@"recharge_pay"]];
            }
        }else{
            cell.detailTextLabel.text = @"";
        }
    }else{
    
        if(_dicData){
            if(indexPath.row == 0){
                cell.detailTextLabel.text = [NSString stringWithFormat:@"%@升",_dicData[@"oil_cost"]];
            }else if (indexPath.row == 1){
                cell.detailTextLabel.text = [NSString stringWithFormat:@"%@次",_dicData[@"order_count"]];
            }else if (indexPath.row == 2){
                cell.detailTextLabel.text = [NSString stringWithFormat:@"%@次",_dicData[@"recharge_times"]];
            }else if (indexPath.row == 3){
                cell.detailTextLabel.text = [NSString stringWithFormat:@"%@元",_dicData[@"recharge_pay"]];
            }
        }else{
            cell.detailTextLabel.text = @"";
        }
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
    whiteView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    UILabel *lab = [QViewUtility createLabelWithFrame:CGRectMake(0,5,kScreenWidth,35) font:16 textAlignment:NSTextAlignmentLeft textColor:[UIColor blackColor] text:@"    账单明细"];
    lab.backgroundColor = [UIColor whiteColor];
    [whiteView addSubview:lab];
    return whiteView;
}

- (void)requestData{

    NSString *url = [QParamService urlWithAction:@"getCusAccountInfo"];
    NSDictionary *param = [QParamService billingDetailstart_time:_start_time end_time:_end_time];
    [QViewUtility showMBHudToView:self.view hideBg:NO];
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            return ;
        }
        
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            _dicData = basModel.items;
            [_tableView reloadData];
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
}



@end
